<?php
namespace App\Admin\Controllers\Basics;

use App\Models\Basics\Currency;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class CurrencyController extends AdminController{

    protected $title = '币种';

    protected function grid()
    {
        $grid = new Grid(new Currency());
        $grid->model()->orderBy('id','DESC');
        $grid->column('id','ID');
        $grid->column('code','币种');
//        $grid->column('name','名称');

        if(Admin::user()->can('currency.edit')){
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableDelete();
            });
            $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                $create->text('code', '币种');
//                $create->text('name', '名称');
            });
        }else{
            $grid->disableActions();
        }

        $grid->disableFilter();
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->quickSearch('code');
        $grid->disableCreateButton();

        return $grid;
    }

    public function form()
    {
        $form = new Form(new Currency());

        $form->hidden('id');

        $form->text('code', '币种')
            ->creationRules(['required', "unique:b_currency"])
            ->updateRules(['required', "unique:b_currency,code,{{id}}"])
            ->width('30%');
        $form->hidden('name');
//        $form->text('name','名称')->required()->rules('required',['required' => '名称不能为空'])->width('30%');
        $form->saving(function (Form $form) {
            if ($form->code && $form->model()->code != $form->code) {
                $form->code = strtoupper($form->code);
            }
            $form->name = $form->code;
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
